import React from 'react';
import Editor from 'for-editor';
import question from '@/services/question';
class EditMarkdown extends React.Component{
    state = {
        value:''
    }

    change = (value) => {
        this.setState({
            value
        })
    }

    addImg = async (file) => {
        console.log(file)
        let formdata = new FormData();
        formdata.append('file',file);
        let res = await question.uploadImage(formdata);
        console.log(process.env)
        this.setState({
            value:`${this.state.value}![alt](${process.env.IMAGE_PATH + res.filename})`
        })
    }

    render(){
        const {value} = this.state;
        return <React.Fragment>
            <Editor
                value={value}
                onChange={this.change}
                addImg = {this.addImg}
                toolbar={{
                    h1: true, // h1
                    h2: true, // h2
                    h3: true, // h3
                    h4: true, // h4
                    img: true, // 图片
                    link: true, // 链接
                    code: true, // 代码块
                    preview: true, // 预览
                    expand: true, // 全屏
                    /* v0.0.9 */
                    undo: true, // 撤销
                    redo: true, // 重做
                    save: true, // 保存
                    /* v0.2.3 */
                    subfield: true, // 单双栏模式
                }}
                >
            </Editor>
        </React.Fragment>
    }

}

export default EditMarkdown